package com.relytosoft.mizar.media.analyze.services.service.face.impl;

import com.relytosoft.mizar.media.alarm.api.entity.Alarm;
import com.relytosoft.mizar.media.alarm.api.feign.AlarmFeignInService;
import com.relytosoft.mizar.media.analyze.services.service.face.AyFaceAlarmService;
import com.relytosoft.mizar.media.analyze.services.service.system.AyUploadImageService;
import com.relytosoft.mizar.media.common.entity.algo.AYVideoSliceCommon;
import com.relytosoft.mizar.media.common.entity.algo.AyControlFace;
import com.relytosoft.mizar.media.common.entity.pojo.dto.AyDispositionPlanDto;
import com.relytosoft.mizar.media.common.entity.pojo.request.btdl.common.BtdlFaceControlReq;
import com.relytosoft.mizar.media.common.enums.UpLoadFileEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author wss
 * @version 1.0
 * @date 2022/6/22 14:05
 */
@Service
public class AyFaceAlarmServiceimpl implements AyFaceAlarmService {

    @Autowired
    AlarmFeignInService alarmFeignInService;
    @Autowired
    AyUploadImageService ayUploadImageService;

    @Override
    public void faceAlarm(AyDispositionPlanDto dispositionPlanDto, Map<String, String> bigPic, AYVideoSliceCommon ayVideoSliceCommon) {
        rawDataUrlReplace(bigPic, ayVideoSliceCommon, UpLoadFileEnum.ALGO_FACE);
        Alarm alarm = Alarm.buildFaceAlarm(dispositionPlanDto, bigPic, ayVideoSliceCommon);
        alarmFeignInService.handTaskAlarm(alarm);
    }

    @Override
    public void faceControlAlarm(AyDispositionPlanDto ayDispositionPlanDto, BtdlFaceControlReq btdlFaceControlReq) {
        controlFaceDataUrlReplace(btdlFaceControlReq);
        Alarm alarm = Alarm.buildFaceControlAlarm(btdlFaceControlReq, ayDispositionPlanDto);
        alarmFeignInService.handTaskAlarm(alarm);
    }

    /**
     * 结构化任务原始数据图片替换
     *
     * @param bigPic
     */
    public void rawDataUrlReplace(Map<String, String> bigPic, AYVideoSliceCommon ayVideoSliceCommon, UpLoadFileEnum upLoadFileEnum) {
        for (String key : bigPic.keySet()) {
            String base64FrameUpload = ayUploadImageService.base64FrameUpload(bigPic.get(key),
                    ayVideoSliceCommon.getLeftTopX(),
                    ayVideoSliceCommon.getLeftTopY(),
                    ayVideoSliceCommon.getRightBtmX(),
                    ayVideoSliceCommon.getRightBtmY(),
                    upLoadFileEnum.getCode());
            bigPic.put(key, base64FrameUpload);
        }
    }

    /**
     * 布控人脸任务数据图片替换
     *
     * @param btdlFaceControlReq
     */
    public void controlFaceDataUrlReplace(BtdlFaceControlReq btdlFaceControlReq) {
        AyControlFace face = btdlFaceControlReq.getgAFace();
        String rawUrlFrameUpload = ayUploadImageService.rawUrlFrameUpload(btdlFaceControlReq.getgAFace().getImage(),
                Integer.parseInt(face.getLeftTopX()),
                Integer.parseInt(face.getLeftTopY()),
                Integer.parseInt(face.getRightBtmX()),
                Integer.parseInt(face.getRightBtmY()),
                UpLoadFileEnum.ALGO_FACE_CONTROL.getCode());
        btdlFaceControlReq.getgAFace().setImage(rawUrlFrameUpload);
    }

}
